package com.chb.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chb.entity.Goods;
import com.chb.entity.Order;
import com.chb.entity.SeckillGoods;
import com.chb.entity.SeckillOrder;
import com.chb.mapper.SeckillGoodsMapper;
import com.chb.utils.UserInfoContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Optional;

/**
 * <p>
 * 秒杀商品表 服务实现类
 * </p>
 *
 * @author CHB
 * @since 2022-08-23 15:58:28
 */
@Service
public class SeckillGoodsService extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> {

    @Resource
    private GoodsService goodsService;
    @Resource
    private SeckillOrderService seckillOrderService;
    @Resource
    private OrderService orderService;

    @Transactional(rollbackFor = Exception.class)
    public String seckillGoods(Long goodsId) {
        Long userId = UserInfoContext.getUserInfo().getUserId();
        // 判断库存
        Goods goods = Optional.ofNullable(goodsService.getById(goodsId)).orElse(new Goods());
        if (goods.getGoodsStock() == null || goods.getGoodsStock() <= 0) {
            return "商品被抢光了";
        }
        // 是否重复秒杀
        SeckillOrder seckillOrder = seckillOrderService.getOne(Wrappers.<SeckillOrder>lambdaQuery()
                .eq(SeckillOrder::getUserId, userId)
                .eq(SeckillOrder::getGoodsId, goodsId).last("limit 1"));
        if (seckillOrder != null) {
            return "商品只能被买一次";
        }
        // 减库存
        goodsService.update(Wrappers.<Goods>lambdaUpdate()
                .eq(Goods::getId, goodsId)
                .set(Goods::getGoodsStock, goods.getGoodsStock() - 1));
        // 生成订单
        Order order = generatorOrder(userId, goods, 1);
        orderService.save(order);
        // 生成秒杀订单
        seckillOrderService.save(generatorSeckillOrder(userId,order.getId(),goodsId));
        return "success";
    }

    /**
     * 生成订单
     * @param userId 用户id
     * @param goods 商品信息
     * @param goodsCount 秒杀数量
     * @author CHB
     * @date 2022-08-23 16:39:14
     * @return Order
     */
    private Order generatorOrder(Long userId, Goods goods, Integer goodsCount) {
        Order order = new Order();
        order.setUserId(userId);
        order.setGoodsId(goods.getId());
        order.setGoodsName(goods.getGoodsName());
        order.setGoodsCount(goodsCount);
        order.setGoodsPrice(goods.getGoodsPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(LocalDateTime.now());
        return order;
    }

    /**
     * 生成秒杀订单信息
     * @param userId 用户id
     * @param orderId 订单id
     * @param goodsId 商品id
     * @author CHB
     * @date 2022-08-23 16:39:46
     * @return SeckillOrder
     */
    private SeckillOrder generatorSeckillOrder(Long userId, Long orderId, Long goodsId) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(userId);
        seckillOrder.setOrderId(orderId);
        seckillOrder.setGoodsId(goodsId);
        return seckillOrder;
    }
}
